`timescale 1ns / 1ps

module sha2_512 #(
    parameter MAX_MSG_LEN = 1024
    )(
    input wire clk,
    input wire rst_n,
//    input ready,
    input wire [MAX_MSG_LEN-1:0] message_block,
    input wire valid,
    output valid_o,
    output reg [511:0] digest
);
    reg done;
    reg [63:0] w [0:79];
    reg [63:0] a, b, c, d, e, f, g, h_temp;
    reg [63:0] T1, T2;
    reg [2:0] state;
    localparam IDLE      = 3'd0,
           PROC      =3'd1,
           FINISH    = 3'd2;
    reg [63:0] h [0:7];
    reg valid_r,valid_r1;
    always @(posedge clk or negedge rst_n)
        if(~rst_n)begin
//            digest_r <= 0;
            valid_r <= 0;
            valid_r1 <=0;
        end
        else begin
//            digest_r <= message_block;
            valid_r <= valid;
            valid_r1 <= valid_r;
        end
        assign valid_o = valid_r1;

    // 常量数组
    reg [63:0] K [0:79];
    integer i;
    always @(*) begin
        if(~rst_n)
            for(i=0;i<80;i=i+1) begin
                K[i] <= 64'h0;
            end
        else begin
            K[0]  = 64'h428a2f98d728ae22; K[1]  = 64'h7137449123ef65cd;
            K[2]  = 64'hb5c0fbcfec4d3b2f; K[3]  = 64'he9b5dba58189dbbc;
            K[4]  = 64'h3956c25bf348b538; K[5]  = 64'h59f111f1b605d019;
            K[6]  = 64'h923f82a4af194f9b; K[7]  = 64'hab1c5ed5da6d8118;
            K[8]  = 64'hd807aa98a3030242; K[9]  = 64'h12835b0145706fbe;
            K[10] = 64'h243185be4ee4b28c; K[11] = 64'h550c7dc3d5ffb4e2;
            K[12] = 64'h72be5d74f27b896f; K[13] = 64'h80deb1fe3b1696b1;
            K[14] = 64'h9bdc06a725c71235; K[15] = 64'hc19bf174cf692694;
            K[16] = 64'he49b69c19ef14ad2; K[17] = 64'hefbe4786384f25e3;
            K[18] = 64'h0fc19dc68b8cd5b5; K[19] = 64'h240ca1cc77ac9c65;
            K[20] = 64'h2de92c6f592b0275; K[21] = 64'h4a7484aa6ea6e483;
            K[22] = 64'h5cb0a9dcbd41fbd4; K[23] = 64'h76f988da831153b5;
            K[24] = 64'h983e5152ee66dfab; K[25] = 64'ha831c66d2db43210;
            K[26] = 64'hb00327c898fb213f; K[27] = 64'hbf597fc7beef0ee4;
            K[28] = 64'hc6e00bf33da88fc2; K[29] = 64'hd5a79147930aa725;
            K[30] = 64'h06ca6351e003826f; K[31] = 64'h142929670a0e6e70;
            K[32] = 64'h27b70a8546d22ffc; K[33] = 64'h2e1b21385c26c926;
            K[34] = 64'h4d2c6dfc5ac42aed; K[35] = 64'h53380d139d95b3df;
            K[36] = 64'h650a73548baf63de; K[37] = 64'h766a0abb3c77b2a8;
            K[38] = 64'h81c2c92e47edaee6; K[39] = 64'h92722c851482353b;
            K[40] = 64'ha2bfe8a14cf10364; K[41] = 64'ha81a664bbc423001;
            K[42] = 64'hc24b8b70d0f89791; K[43] = 64'hc76c51a30654be30;
            K[44] = 64'hd192e819d6ef5218; K[45] = 64'hd69906245565a910;
            K[46] = 64'hf40e35855771202a; K[47] = 64'h106aa07032bbd1b8;
            K[48] = 64'h19a4c116b8d2d0c8; K[49] = 64'h1e376c085141ab53;
            K[50] = 64'h2748774cdf8eeb99; K[51] = 64'h34b0bcb5e19b48a8;
            K[52] = 64'h391c0cb3c5c95a63; K[53] = 64'h4ed8aa4ae3418acb;
            K[54] = 64'h5b9cca4f7763e373; K[55] = 64'h682e6ff3d6b2b8a3;
            K[56] = 64'h748f82ee5defb2fc; K[57] = 64'h78a5636f43172f60;
            K[58] = 64'h84c87814a1f0ab72; K[59] = 64'h8cc702081a6439ec;
            K[60] = 64'h90befffa23631e28; K[61] = 64'ha4506cebde82bde9;
            K[62] = 64'hbef9a3f7b2c67915; K[63] = 64'hc67178f2e372532b;
            K[64] = 64'hca273eceea26619c; K[65] = 64'hd186b8c721c0c207; 
            K[66] = 64'heada7dd6cde0eb1e; K[67] = 64'hf57d4f7fee6ed178; 
            K[68] = 64'h06f067aa72176fba; K[69] = 64'h0a637dc5a2c898a6;
            K[70] = 64'h113f9804bef90dae; K[71] = 64'h1b710b35131c471b; 
            K[72] = 64'h28db77f523047d84; K[73] = 64'h32caab7b40c72493;        
            K[74] = 64'h3c9ebe0a15c9bebc; K[75] = 64'h431d67c49c100d4c; 
            K[76] = 64'h4cc5d4becb3e42b6; K[77] = 64'h597f299cfc657e2a; 
            K[78] = 64'h5fcb6fab3ad6faec; K[79] = 64'h6c44198c4a475817;
        end
    end

    function [63:0] Ch;
        input [63:0] x, y, z;
        Ch = (x & y) ^ (~x & z);
    endfunction

    function [63:0] Maj;
        input [63:0] x, y, z;
        Maj = (x & y) ^ (x & z) ^ (y & z);
    endfunction

    function [63:0] Sigma0;
        input [63:0] x;
        Sigma0 = ((x >> 28) | (x << 36)) ^ ((x >> 34) | (x << 30)) ^ ((x >> 39) | (x << 25));
    endfunction

    function [63:0] Sigma1;
        input [63:0] x;
        Sigma1 = ((x >> 14) | (x << 50)) ^ ((x >> 18) | (x << 46)) ^ ((x >> 41) | (x << 23));
    endfunction

    function [63:0] sigma0;
        input [63:0] x;
        sigma0 = ((x >> 1) | (x << 63)) ^ ((x >> 8) | (x << 56)) ^ (x >> 7);
    endfunction

    function [63:0] sigma1;
        input [63:0] x;
        sigma1 = ((x >> 19) | (x << 45)) ^ ((x >> 61) | (x << 3)) ^ (x >> 6);
    endfunction


    
    
    // 压缩循环
    integer t;
    always @(*) 
        if (!rst_n) begin
//            for (t = 0; t < 64; t = t + 1) begin
//                w[t] =  0;
//            end
            h[0] = 32'h0;h[1] = 32'h0;h[2] = 32'h0;h[3] = 32'h0;
            h[4] = 32'h0;h[5] = 32'h0;h[6] = 32'h0;h[7] = 32'h0;
            a = 0;
            b = 0;
            c = 0;
            d = 0;
            e = 0;
            f = 0;
            g = 0;
            h_temp =0;
            T1=0;
            T2=0;
            digest <= 256'b0;
//            valid_r <= 0;
        end
        else if(state==IDLE)begin
            h[0] = 64'h6a09e667f3bcc908; 
            h[1] = 64'hbb67ae8584caa73b; 
            h[2] = 64'h3c6ef372fe94f82b; 
            h[3] = 64'ha54ff53a5f1d36f1; 
            h[4] = 64'h510e527fade682d1; 
            h[5] = 64'h9b05688c2b3e6c1f; 
            h[6] = 64'h1f83d9abfb41bd6b; 
            h[7] = 64'h5be0cd19137e2179; 
            for (t = 0; t < 16; t = t + 1) begin
                w[t] <= message_block[(MAX_MSG_LEN-1 - t * 64) -: 64];
        //                    w[t] <= message_block[(MAX_MSG_LEN-1 - t * 32) -: 32];
            end
            for (t = 16; t < 80; t = t + 1) begin
                w[t] <= sigma1(w[t - 2]) + w[t - 7] + sigma0(w[t - 15]) + w[t - 16];
            end
        end     
        else if(state == PROC) begin
            a = h[0];
            b = h[1];
            c = h[2];
            d = h[3];
            e = h[4];
            f = h[5];
            g = h[6];
            h_temp = h[7];
            for (t = 0; t < 80; t = t + 1) begin
                    T1 = h_temp + Sigma1(e) + Ch(e, f, g) + K[t] + w[t];
                    T2 = Sigma0(a) + Maj(a, b, c);
                    h_temp = g;
                    g = f;
                    f = e;
                    e = d + T1;
                    d = c;
                    c = b;
                    b = a;
                    a = T1 + T2;
                end

                h[0] = h[0] + a;
                h[1] = h[1] + b;
                h[2] = h[2] + c;
                h[3] = h[3] + d;
                h[4] = h[4] + e;
                h[5] = h[5] + f;
                h[6] = h[6] + g;
                h[7] = h[7] + h_temp;
            digest = {h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]};
        end 
        
    always @(posedge clk or negedge rst_n) 
            if (!rst_n) begin
                state <= IDLE;
                done <= 1'b0;
            end
            else begin
//                valid_r <= valid;
                case(state)
                IDLE:begin
                    done <= 1'b0;
                    if(valid_r) begin    
                        state <= PROC;
                        done <= 1'b0;end
                end
                PROC:begin
                    state <= IDLE;
                    done <= 1'd1;
                    if(done)
                        done <= 0;
                end
                default:begin
                    state <= IDLE;
                    done <= 1'b0;
                end
                endcase
            end
                        
       assign valid_o = valid_r1;

endmodule    